package com.example.demo.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.example.demo.custom.BaseService;
import com.example.demo.entity.BanZu;
import com.example.demo.entity.SysDept;
import com.example.demo.entity.SysRole;
import com.example.demo.entity.SysUser;
import com.example.demo.repository.SysUserRepository;
import com.example.demo.security.User2;

@Service
public class SysUserService extends BaseService<SysUser, Integer> implements UserDetailsService {
	@Autowired
	private SysUserRepository userDAO;

	public SysUser login(String username, String password) {
		return userDAO.findByUsernameAndPassword(username, password);
	}

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		SysUser user=userDAO.findByUsername(username);
		if(user==null) {
			throw new UsernameNotFoundException(username);
		}
		List<GrantedAuthority> authorities=new ArrayList<GrantedAuthority>();
		List<SysRole> roles=user.getRoles();
		for (SysRole role : roles) {
			authorities.add(new SimpleGrantedAuthority(role.getCode()));
		}
		return new User2(user.getUsername(), user.getPassword(), user.getName(), authorities);
	}

	public <S extends SysUser> S save(S entity) {
		return userDAO.save(entity);
	}


	public Page<SysUser> findAll(Pageable pageable) {
		return userDAO.findAll(pageable);
	}

	public List<SysUser> findAll() {
		return userDAO.findAll();
	}

	public List<SysUser> findAll(Sort sort) {
		return userDAO.findAll(sort);
	}

	public List<SysUser> findAll(Specification<SysUser> spec) {
		return userDAO.findAll(spec);
	}

	public List<SysUser> findAllById(Iterable<Integer> ids) {
		return userDAO.findAllById(ids);
	}

	public Page<SysUser> findAll(Specification<SysUser> spec, Pageable pageable) {
		return userDAO.findAll(spec, pageable);
	}

	public <S extends SysUser> List<S> saveAll(Iterable<S> entities) {
		return userDAO.saveAll(entities);
	}

	public List<SysUser> findAll(Specification<SysUser> spec, Sort sort) {
		return userDAO.findAll(spec, sort);
	}

	public <S extends SysUser> S saveAndFlush(S entity) {
		return userDAO.saveAndFlush(entity);
	}

	public <S extends SysUser> Page<S> findAll(Example<S> example, Pageable pageable) {
		return userDAO.findAll(example, pageable);
	}

	public void deleteAllInBatch() {
		userDAO.deleteAllInBatch();
	}

	public SysUser getOne(Integer id) {
		return userDAO.getOne(id);
	}

	public <S extends SysUser> List<S> findAll(Example<S> example) {
		return userDAO.findAll(example);
	}

	public void deleteById(Integer id) {
		userDAO.deleteById(id);
	}

	public <S extends SysUser> List<S> findAll(Example<S> example, Sort sort) {
		return userDAO.findAll(example, sort);
	}

	public void delete(SysUser entity) {
		userDAO.delete(entity);
	}

	public void deleteAll(Iterable<? extends SysUser> entities) {
		userDAO.deleteAll(entities);
	}

	public void deleteAll() {
		userDAO.deleteAll();
	}
	public SysUser findById(Integer id) {
		Optional<SysUser> optional = userDAO.findById(id);
		if(optional.isPresent()) {
			return optional.get();
		}
		return null;
	}

	public SysUser findByUsername(String username) {
		return userDAO.findByUsername(username);
	}

	public List<SysUser> findByDeptId(Integer nameId) {
		return userDAO.findByDeptId(nameId);
		
	}

	public List<SysUser> findByDeptName(String name) {
		return userDAO.findByDeptName(name);
		
	}

	public List<SysUser> findByBanZu(BanZu banzu) {
		return userDAO.findByBanzu(banzu);
		
	}



}
